package com.gyh.stlserver.controller;

import cn.hutool.core.util.ObjectUtil;
import com.aliyuncs.exceptions.ClientException;
import com.github.pagehelper.PageInfo;
import com.gyh.stlserver.constants.Constants;
import com.gyh.stlserver.entity.Admin;
import com.gyh.stlserver.entity.Coach;
import com.gyh.stlserver.service.AdminService;
import com.gyh.stlserver.service.CoachService;
import com.gyh.stlserver.util.OssUtil;
import com.gyh.stlserver.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/coach")
public class CoachController {
    @Autowired
    private CoachService coachService;

    @GetMapping
    public Result search(Integer pageNum, Integer pageSize, Coach coach){
        if (ObjectUtil.isEmpty(pageNum)&&ObjectUtil.isEmpty(pageSize)){
            List<Coach> coaches = coachService.search(coach);
            return Result.success(Constants.STATUS_200,coaches);
        }
        PageInfo<Coach> pageInfo = coachService.searchByPage(pageNum, pageSize, coach);
        return Result.success(Constants.STATUS_200,pageInfo);
    }
    /*添加教练*/
    @PostMapping("/addCoach")
    public Result addCoach(@RequestBody Coach coach ){
        int i = coachService.addCoach(coach);
        if (i<1){
            return Result.error(Constants.STATUS_401,"添加失败");
        }
        return Result.success(Constants.STATUS_200);
    }
    /*校验手机*/
    @GetMapping("/checkPhone")
    public Result checkPhone(String phone){
        Coach coach =  coachService.getCoachByPhone(phone);
        if (ObjectUtil.isNotEmpty(coach)){
            return Result.error(Constants.STATUS_402,"手机号已存在");
        }
        return Result.success(Constants.STATUS_200);
    }
    /*删除教练*/
    @GetMapping("/deleteCoach")
    public Result deleteCoach(Integer id){
        int i =  coachService.removeCoach(id);
        if (i==-2){
            return Result.error(Constants.STATUS_401,"该教练还有未完成的课程，不能删除");
        }
        if (i<1){
            return Result.error(Constants.STATUS_401,"删除失败");
        }
        return Result.success(Constants.STATUS_200);
    }
    /*编辑教练*/
    @PostMapping("/editCoach")
    public Result editCoach(@RequestBody Coach coach){
        int i =  coachService.editCoach(coach);
        if (i<1){
            return Result.error(Constants.STATUS_401,"编辑失败");
        }
        return Result.success(Constants.STATUS_200);
    }
    /*校验编辑手机号*/
    @PostMapping("/checkEditPhone")
    public Result checkEditPhone(@RequestBody Coach coach){
        Coach dbCoach =  coachService.getCoachByPhone(coach.getPhone());
        if (ObjectUtil.isEmpty(dbCoach)) return Result.success(Constants.STATUS_200);
        if (!dbCoach.getId().equals(coach.getId())){
            return Result.error(Constants.STATUS_402,"手机号已存在");
        }
        return Result.success(Constants.STATUS_200);
    }
    /*批量删除*/
    @PostMapping("/delSelected")
    public Result delSelected(@RequestBody Integer[] ids){
        int i = coachService.delSelected(ids);
        if (i<1){
            return Result.error(Constants.STATUS_401,"删除失败");
        }
        return Result.success(Constants.STATUS_200);
    }

}
